﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Amf4Asmx.Configuration
{
    public sealed class ServiceHandlersSection : ConfigurationSection
    {
        // Fields
        private bool _validated;

        // Methods
        static ServiceHandlersSection()
        {
        }

        internal ServiceHandlerMapping FindMapping(string contentType)
        {
            this.ValidateHandlers();
            for (int i = 0; i < this.Handlers.Count; i++)
            {
                ServiceHandlerMapping action = this.Handlers[i];
                if (action.IsMatch(contentType))
                {
                    return action;
                }
            }
            return null;
        }

        #region DefuaultType

        private Type _type;
        private string typeCache;

        [ConfigurationProperty("defaultType")]
        public string DefaultType
        {
            get
            {
                if (this.typeCache == null)
                {
                    this.typeCache = (string)base["defaultType"];
                }
                return this.typeCache;
            }
            set
            {
                base["defaultType"] = value;
                this.typeCache = value;
            }
        }

        internal Type TypeInternal
        {
            get
            {
                if (this._type == null)
                {
                    if (string.IsNullOrEmpty(this.DefaultType))
                    {
                        this._type = typeof(System.Web.Services.Protocols.WebServiceHandlerFactory);
                    }
                    else
                    {
                        this._type = System.Type.GetType(this.DefaultType);
                        if (this._type == null)
                        {
                            throw new ConfigurationErrorsException(string.Format("Could not find type {0}", this.DefaultType), base.ElementInformation.Source, base.ElementInformation.LineNumber);
                        }
                        if (!Utils.IsTypeHandlerOrFactory(this._type))
                        {
                            throw new ConfigurationErrorsException(string.Format("Type {0} does not implement either IHttpHandler or IHttpHandlerFactory", this.DefaultType), base.ElementInformation.Source, base.ElementInformation.LineNumber);
                        }
                    }
                }
                return this._type;
            }
        }
        #endregion

        internal bool ValidateHandlers()
        {
            if (!this._validated)
            {
                lock (this)
                {
                    if (!this._validated)
                    {
                        foreach (ServiceHandlerMapping action in this.Handlers)
                        {
                            action.InitValidateInternal();
                        }
                        this._validated = true;
                    }
                }
            }
            return this._validated;
        }

        // Properties
        [ConfigurationProperty("", IsDefaultCollection=true)]
        public ServiceHandlerMappingCollection Handlers
        {
            get
            {
                return (ServiceHandlerMappingCollection) base[""];
            }
        }

    }

 
 

}
